package com.yhd.gmall.list.controller;

import com.yhd.gmall.common.result.Result;
import com.yhd.gmall.list.service.SearchService;
import com.yhd.gmall.model.list.Goods;
import com.yhd.gmall.model.list.SearchParam;
import com.yhd.gmall.model.list.SearchResponseVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author yhd
 * @createtime 2020/9/22 12:21
 */
@RestController
@RequestMapping("api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @Autowired
    private SearchService searchService;

    @ApiOperation("初始化索引到es中")
    @GetMapping("inner/createIndex")
    public Result createIndex() {
        restTemplate.createIndex(Goods.class);
        restTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    @ApiOperation("往es里添加商品")
    @GetMapping("inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long skuId) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    @ApiOperation("从es往出拿商品")
    @GetMapping("inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }
    @ApiOperation("热点商品")
    @GetMapping("inner/incrHotScore/{skuId}")
    public Result incrHotScore(Long skuId){
        searchService.incrHotScore(skuId);
        return Result.ok();
    }
    @ApiOperation("搜索商品")
    @PostMapping
    public Result list(@RequestBody SearchParam searchParam) throws IOException {
        SearchResponseVo response = searchService.search(searchParam);
        return Result.ok(response);
    }

}